package com.jiugang.cardump.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiugang.cardump.entity.Alert;
import com.jiugang.cardump.mapper.AlertMapper;
import com.jiugang.cardump.result.ResultInfoV110;
import com.jiugang.cardump.service.AlertService;
import com.jiugang.cardump.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2021-08-19
 */
@Service
@Slf4j
class AlertServiceImpl implements AlertService {

    @Autowired
    private AlertMapper alertMapper;

    @Override
    public ResultInfoV110 selectEquipmentAlerAlltList(String equipmentId, Integer page, Integer size, String StartTime, String endTime) {
        log.info("AlertServiceImpl的selectEquipmentAlertList的方法进入,参数equipmentId为" + equipmentId);

        QueryWrapper<Alert> alertQueryWrapper = new QueryWrapper<>();
        alertQueryWrapper.eq("id", equipmentId)
                .ge(!StrUtil.hasEmpty( StartTime), "sjsj", StartTime)
                .le(!StrUtil.hasEmpty( endTime), "sjsj", endTime)
                .orderByDesc("sjsj");

        Page<Alert> alertPage = new Page<>(page, size);
        Page<Alert> alertIPage = alertMapper.selectPage(alertPage, alertQueryWrapper);
        HashMap<Object, Object> resultMap = new HashMap<>();
        List<Alert> records = alertIPage.getRecords();
        resultMap.put("list", records);
        resultMap.put("page", page);
        resultMap.put("size", size);
        resultMap.put("total", alertPage.getTotal());
        resultMap.put("totalPage", alertPage.getPages());

        return ResultInfoV110.success(resultMap);
    }

    /**
     * @param id 设备Id
     * @return ResultInfoV110
     * @Description 根据设备Id查询报警状态
     */
    @Override

    public ResultInfoV110 selectEquipmentAlertList(String id) {
        log.info("AlertServiceImpl的selectEquipmentAlertList的方法进入,参数equipmentId为" + id);
        //根据设备Id查询报警状态,显示最新30条记录
        List<Alert> alerts =
                alertMapper.selectEquipmentAlertList(id);
        return ResultInfoV110.success(alerts);
    }

    /**
     * @param startTime 搜索初始时间
     * @param endTime   搜索结束时间
     * @param tableId
     * @return
     * @Description 统计过去一小时内的报警数量, 将过去过去一小时分成12个等分区间
     */
    @Override
    public ResultInfoV110 staticticsAlertInOneHour(String equipmentId) {
        log.info("AlertServiceImpl的staticticsAlertInOneHour的方法进入,参数tableId为" + equipmentId);

        List<String> timeRangeList = new ArrayList<>();
        List<Integer> alertCountList = new ArrayList<>();

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //将过去的一个小时等分为12等份
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime nowMinus5 = LocalDateTime.parse(TimeUtil.dateRoll(5), dtf);
        LocalDateTime nowMinus10 = LocalDateTime.parse(TimeUtil.dateRoll(10), dtf);
        LocalDateTime nowMinus15 = LocalDateTime.parse(TimeUtil.dateRoll(15), dtf);
        LocalDateTime nowMinus20 = LocalDateTime.parse(TimeUtil.dateRoll(20), dtf);
        LocalDateTime nowMinus25 = LocalDateTime.parse(TimeUtil.dateRoll(25), dtf);
        LocalDateTime nowMinus30 = LocalDateTime.parse(TimeUtil.dateRoll(30), dtf);
        LocalDateTime nowMinus35 = LocalDateTime.parse(TimeUtil.dateRoll(35), dtf);
        LocalDateTime nowMinus40 = LocalDateTime.parse(TimeUtil.dateRoll(40), dtf);
        LocalDateTime nowMinus45 = LocalDateTime.parse(TimeUtil.dateRoll(45), dtf);
        LocalDateTime nowMinus50 = LocalDateTime.parse(TimeUtil.dateRoll(50), dtf);
        LocalDateTime nowMinus55 = LocalDateTime.parse(TimeUtil.dateRoll(55), dtf);
        LocalDateTime nowMinus60 = LocalDateTime.parse(TimeUtil.dateRoll(60), dtf);
        //封装
        timeRangeList.add(dtf.format(now));
        timeRangeList.add(TimeUtil.dateRoll(5));
        timeRangeList.add(TimeUtil.dateRoll(10));
        timeRangeList.add(TimeUtil.dateRoll(15));
        timeRangeList.add(TimeUtil.dateRoll(20));
        timeRangeList.add(TimeUtil.dateRoll(25));
        timeRangeList.add(TimeUtil.dateRoll(30));
        timeRangeList.add(TimeUtil.dateRoll(35));
        timeRangeList.add(TimeUtil.dateRoll(40));
        timeRangeList.add(TimeUtil.dateRoll(45));
        timeRangeList.add(TimeUtil.dateRoll(50));
        timeRangeList.add(TimeUtil.dateRoll(55));


        //过去0-5分钟内该设备报警次数
        Integer integer = alertMapper.staticticsAlertInOneHour(nowMinus5, now, equipmentId);
        //过去5-10分钟内该设备报警次数
        Integer integer1 = alertMapper.staticticsAlertInOneHour(nowMinus10, nowMinus5, equipmentId);
        //过去10-15分钟内该设备报警次数
        Integer integer2 = alertMapper.staticticsAlertInOneHour(nowMinus15, nowMinus10, equipmentId);
        //过去15-20分钟内该设备报警次数
        Integer integer3 = alertMapper.staticticsAlertInOneHour(nowMinus20, nowMinus15, equipmentId);
        //过去20-25分钟内该设备报警次数
        Integer integer4 = alertMapper.staticticsAlertInOneHour(nowMinus25, nowMinus20, equipmentId);
        //过去25-30分钟内该设备报警次数
        Integer integer5 = alertMapper.staticticsAlertInOneHour(nowMinus30, nowMinus25, equipmentId);
        //过去30-35分钟内该设备报警次数
        Integer integer6 = alertMapper.staticticsAlertInOneHour(nowMinus35, nowMinus30, equipmentId);
        //过去35-40分钟内该设备报警次数
        Integer integer7 = alertMapper.staticticsAlertInOneHour(nowMinus40, nowMinus35, equipmentId);
        //过去40-45分钟内该设备报警次数
        Integer integer8 = alertMapper.staticticsAlertInOneHour(nowMinus45, nowMinus40, equipmentId);
        //过去45-50分钟内该设备报警次数
        Integer integer9 = alertMapper.staticticsAlertInOneHour(nowMinus50, nowMinus45, equipmentId);
        //过去50-55分钟内该设备报警次数
        Integer integer10 = alertMapper.staticticsAlertInOneHour(nowMinus55, nowMinus50, equipmentId);
        //过去50-55分钟.a内该设备报警次数
        Integer integer11 = alertMapper.staticticsAlertInOneHour(nowMinus60, nowMinus55, equipmentId);

        alertCountList.add(integer);
        alertCountList.add(integer1);
        alertCountList.add(integer2);
        alertCountList.add(integer3);
        alertCountList.add(integer4);
        alertCountList.add(integer5);
        alertCountList.add(integer6);
        alertCountList.add(integer7);
        alertCountList.add(integer8);
        alertCountList.add(integer9);
        alertCountList.add(integer10);
        alertCountList.add(integer11);

        //封装分页信息
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("xAxis", timeRangeList);
        resultMap.put("data", alertCountList);
        return ResultInfoV110.success(resultMap);
    }
}
